package com.ubs.lingua.buildtool.cs;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CsBuildAgentEngine implements Engine, Runnable {

    private static final Log LOG = LogFactory.getLog(CsBuildAgentEngine.class);

    private AbstractApplicationContext context;

    @Override
    public void run() {
        synchronized (this) {
            LOG.info("Starting Spring application context");
            context = new ClassPathXmlApplicationContext("META-INF/spring/app-context.xml");
            context.registerShutdownHook();
        }
    }

    @Override
    public void stop() {
        synchronized (this) {
            LOG.info("Closing Spring application context");
            context.close();
        }
    }

    @Override
    public boolean isStopped() {
        synchronized (this) {
            return !context.isActive();
        }
    }

}
